package com.skt.common_hr.overtime.view
{
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.overtime.model.HolidayProxy;
	import com.skt.common_hr.overtime.model.MonthlyDeptListProxy;
	import com.skt.common_hr.overtime.model.MonthlyMemberListProxy;
	import com.skt.common_hr.overtime.model.vo.MonthlyDeptVO;
	import com.skt.common_hr.overtime.view.components.MemberOtListView;
	import com.skt.common_hr.overtime.view.components.MemberOtView;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event; 
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class MemberOtListMediator extends CommonMediator
	{
		public static const NAME:String = "MemberOtListMediator"; 
		
		public static const SAVE_MEMBER_OT_COMPLETE:String = "saveMemberOtComplete";
		public static const CLOSE_MEMBER_OT_LIST_VIEW:String = "closeMemberOtlISTView";
		
		public var bExecMemberOt:Boolean = false;
		private var jichakList:Array = ["712", "713", "714", "715", "718", "720", "721", "730", "805", "831", "833", "835", "841"];
		

		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function MemberOtListMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			setEventListener();
 		}


		private function setEventListener():void
		{
 			// 각 버튼들의 Event Handler를 설정하고
 			view.addEventListener(MemberOtListView.CHANGED_DATE, onChangedDate);
 			view.addEventListener(MemberOtListView.CHANGED_DEPT, onChangedDept);
 			view.addEventListener(MemberOtListView.CLICK_DATE, onClickDate);
 			view.addEventListener(MemberOtListView.CANCEL, onClickCancel);
 			view.addEventListener(MemberOtListView.CHANGED_MONTH, onChangedMonth);
		}

		private var monthlyDeptVO:MonthlyDeptVO = null;
		
 		public function exec(month:String, selectedDeptVO:MonthlyDeptVO, deptList:ArrayCollection):void
 		{
 			bActivated = true;
 			monthlyDeptVO = selectedDeptVO;
 			view.otList = new ArrayCollection();
 			view.deptList = deptList;			// 부서 콤보에  dp로 설정해주고
 			setDeptIndex(selectedDeptVO.ORGEH);			// 선택된 부서를 선택할 수 있게
			view.setMonth(month);				// 선택된 월으로 세팅
			loadList(month, selectedDeptVO.ORGEH);
			view.setHoliday();
 		}
 		/*
 		public function exec(month:String, selectedDept:String, deptList:ArrayCollection):void
 		{
 			bActivated = true;
 			view.otList = new ArrayCollection();
 			view.deptList = deptList;			// 부서 콤보에  dp로 설정해주고
 			setDeptIndex(selectedDept);			// 선택된 부서를 선택할 수 있게
			view.setMonth(month);				// 선택된 월으로 세팅
			loadList(month, selectedDept);
			view.setHoliday();
 		}
		*/
		private function loadList(month:String, dept:String, bSub:Boolean=false):void 
		{
			var proxy:MonthlyMemberListProxy = facade.retrieveProxy(MonthlyMemberListProxy.NAME) as MonthlyMemberListProxy;
			proxy.load(month, dept, bSub);
			
			monthlyDeptVO = null;

			var deptProxy:MonthlyDeptListProxy = facade.retrieveProxy(MonthlyDeptListProxy.NAME) as MonthlyDeptListProxy;
			deptProxy.loadInfo(dept, month);
		}
		
		
		private function onChangedMonth(e:Event):void
		{
			var month:String = view.selectedYear.toString();
			month += (view.selectedMonth<10) ? "0"+view.selectedMonth.toString():view.selectedMonth.toString();
			loadList(month, view.selectedDept, view.chkSubDept.selected);
		}

		private function onChangedDate(e:Event):void
		{
			
		}
		
		private function onChangedDept(e:Event):void
		{
			var month:String = view.selectedYear.toString();
			month += (view.selectedMonth<10) ? "0"+view.selectedMonth.toString():view.selectedMonth.toString();
			loadList(month, view.selectedDept, view.chkSubDept.selected);
		}
		
		private function onClickCancel(e:Event):void
		{
			closeView();
		}
		
		private function onClickDate(e:Event):void
		{
			execMemberOT();
		}
		
		private function setDeptIndex(dept:String):void
		{
 			var month:String = view.selectedYear.toString();
			month += (view.selectedMonth<10) ? "0"+view.selectedMonth.toString():view.selectedMonth.toString();
			for(var i:int=0; i<view.deptList.length; i++) {
				if(view.deptList.getItemAt(i).data == dept) {
					view.cbDept.selectedIndex = i;
					view.selectedDept = dept;
					return;
				}
			}
		}

		private function accessableDeptList():ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection();
				var vo:Object = new Object();
				vo.label = view.cbDept.selectedItem.label;
				vo.data =  view.cbDept.selectedItem.data;
				list.addItem(vo);
			return list;
		}





		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					MonthlyMemberListProxy.LOAD_MONTHLY_MEMBER_LIST_COMPLETE,
					MonthlyMemberListProxy.LOAD_MONTHLY_MEMBER_LIST_FAILED,
					MonthlyDeptListProxy.LOAD_MONTHLY_DEPT_INFO_COMPLETE,
					MonthlyDeptListProxy.LOAD_MONTHLY_DEPT_INFO_FAILED,
					MemberOtMediator.CLOSE_MEMBER_OT_VIEW,
					HolidayProxy.LOAD_HOLIDAYS_COMPLETE
					];
		}
		
		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			if(bActivated) {
				switch(note.getName()) 
				{
					case MonthlyMemberListProxy.LOAD_MONTHLY_MEMBER_LIST_COMPLETE:
						view.otList = new ArrayCollection();
						view.otList = procList((note.getBody() as MonthlyMemberListProxy).list);
						view.bReadOnly = (note.getBody() as MonthlyMemberListProxy).bReadOnly;
						//view.otList.addItemAt(getDeptOt(), 0);
						break;
					case MonthlyMemberListProxy.LOAD_MONTHLY_MEMBER_LIST_FAILED:
						view.otList = new ArrayCollection();
						view.bReadOnly = true;
						var msg:String = "(" + (note.getBody() as MonthlyMemberListProxy).retCode +")";
						msg += (note.getBody() as MonthlyMemberListProxy).retText;
						CommonAlert.showMessage("", msg, null, "오류");
						break;
					case MonthlyDeptListProxy.LOAD_MONTHLY_DEPT_INFO_COMPLETE:
						monthlyDeptVO = (note.getBody() as MonthlyDeptListProxy).deptInfo;
						if(monthlyDeptVO!= null && view.otList!=null && view.otList.length>0) {
							view.otList = procList(view.otList);
						}
						break;
					case MonthlyDeptListProxy.LOAD_MONTHLY_DEPT_INFO_FAILED:
						break;
					case MemberOtMediator.CLOSE_MEMBER_OT_VIEW:
						if(bExecMemberOt) {
							bExecMemberOt = false;
							var month:String = view.selectedYear.toString();
							month += (view.selectedMonth<10) ? "0"+view.selectedMonth: view.selectedMonth;
							loadList(month, view.selectedDept);
						}
						break;
					case HolidayProxy.LOAD_HOLIDAYS_COMPLETE:
						view.holidays = new ArrayCollection((note.getBody() as HolidayProxy).list);
						view.setHoliday();
						break;
				}
			}
		}
		
		
		private function procList(src:ArrayCollection):ArrayCollection
		{
			if(StringUtil.NVL(src.getItemAt(0).PERNR) == "") {
				src.getItemAt(0).ENAME = view.cbDept.selectedLabel;
				if(monthlyDeptVO!=null) {
					src.getItemAt(0).sum = monthlyDeptVO.sum;
					src.getItemAt(0).DAY01 = monthlyDeptVO.DAY01;
					src.getItemAt(0).DAY02 = monthlyDeptVO.DAY02;
					src.getItemAt(0).DAY03 = monthlyDeptVO.DAY03;
					src.getItemAt(0).DAY04 = monthlyDeptVO.DAY04;
					src.getItemAt(0).DAY05 = monthlyDeptVO.DAY05;
					src.getItemAt(0).DAY06 = monthlyDeptVO.DAY06;
					src.getItemAt(0).DAY07 = monthlyDeptVO.DAY07;
					src.getItemAt(0).DAY08 = monthlyDeptVO.DAY08;
					src.getItemAt(0).DAY09 = monthlyDeptVO.DAY09;
					src.getItemAt(0).DAY10 = monthlyDeptVO.DAY10;
					src.getItemAt(0).DAY11 = monthlyDeptVO.DAY11;
					src.getItemAt(0).DAY12 = monthlyDeptVO.DAY12;
					src.getItemAt(0).DAY13 = monthlyDeptVO.DAY13;
					src.getItemAt(0).DAY14 = monthlyDeptVO.DAY14;
					src.getItemAt(0).DAY15 = monthlyDeptVO.DAY15;
					src.getItemAt(0).DAY16 = monthlyDeptVO.DAY16;
					src.getItemAt(0).DAY17 = monthlyDeptVO.DAY17;
					src.getItemAt(0).DAY18 = monthlyDeptVO.DAY18;
					src.getItemAt(0).DAY19 = monthlyDeptVO.DAY19;
					src.getItemAt(0).DAY20 = monthlyDeptVO.DAY20;
					src.getItemAt(0).DAY21 = monthlyDeptVO.DAY21;
					src.getItemAt(0).DAY22 = monthlyDeptVO.DAY22;
					src.getItemAt(0).DAY23 = monthlyDeptVO.DAY23;
					src.getItemAt(0).DAY24 = monthlyDeptVO.DAY24;
					src.getItemAt(0).DAY25 = monthlyDeptVO.DAY25;
					src.getItemAt(0).DAY26 = monthlyDeptVO.DAY26;
					src.getItemAt(0).DAY27 = monthlyDeptVO.DAY27;
					src.getItemAt(0).DAY28 = monthlyDeptVO.DAY28;
					src.getItemAt(0).DAY29 = monthlyDeptVO.DAY29;
					src.getItemAt(0).DAY30 = monthlyDeptVO.DAY30;
					src.getItemAt(0).DAY31 = monthlyDeptVO.DAY31;
				}
			}
			
			/*
			if(src.length>1) {
				for(var i:int=0; i<src.length; i++) {
					if( jichakList.indexOf(src.getItemAt(i).JIKCHAK) >=0 ) {
						src.addItemAt(src.removeItemAt(i), 1);
					}
				}
			}
			*/
			return src;
		}
 		private function closeView(cancel:Boolean=false):void
		{
			bActivated = false;
			facade.sendNotification(CLOSE_MEMBER_OT_LIST_VIEW, this);
		}
 		




		private var memberOtView:MemberOtView = null;
		private function execMemberOT():void
		{
			var memberOtMediator:MemberOtMediator = facade.retrieveMediator(MemberOtMediator.NAME) as MemberOtMediator;
			bExecMemberOt = true;
			if(memberOtMediator==null) {
				memberOtView = PopUpManager.createPopUp(view.root, MemberOtView, true) as MemberOtView;
				PopUpManager.centerPopUp(memberOtView);
				
				memberOtMediator = new MemberOtMediator(memberOtView);
				facade.registerMediator(memberOtMediator);
			}
			else {
				PopUpManager.addPopUp(memberOtMediator.view, view.root, true);
			}
			
			memberOtMediator.exec(view.selectedDate, view.selectedDept, getDeptOt(), accessableDeptList());
		}
		
		private function getDeptOt():Number
		{
			var ot:Number = 0;

			var otField:String = "DAY";
			otField += (view.selectedDate.date<10) ? "0"+view.selectedDate.date:view.selectedDate.date;
			ot = parseInt(view.otList.getItemAt(0)[otField]);
			if(isNaN(ot)) ot = 0;

			return ot;
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        public function get view():MemberOtListView
        {
            return viewComponent as MemberOtListView;
        }
	}
}